/**
 * 
 */
package com.shengruan.user.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.shengruan.admin.jdbc.UserAccountJdbc;
import com.shengruan.admin.jdbc.UserJdbc;
import com.shengruan.admin.vo.Product;
import com.shengruan.admin.vo.User;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;
import com.shengruan.index.action.AuctionUtil;
import com.shengruan.user.jdbc.AutoAuctionJdbc;
import com.shengruan.user.vo.AutoAuction;

/**
 * @author liubin
 *
 */
public class AutoAuctionAction extends BaseAction{
	protected static Logger logger = Logger.getLogger(AutoAuctionAction.class);
	private AutoAuctionJdbc autoAuctionJdbc = (AutoAuctionJdbc)this.getJdbc(AutoAuctionJdbc.class);
	private UserAccountJdbc userAccountJdbc = (UserAccountJdbc)this.getJdbc(UserAccountJdbc.class);

	/***
	 * 获得我的自动竞拍列表
	 */
	public void getList() throws Exception
	{
		User user = this.getCurrentUser();
		int userId = user.getId();
		List<AutoAuction> aList = autoAuctionJdbc.getList(userId, 1);
		List<AutoAuction> bList = autoAuctionJdbc.getList(userId, 0);
		this.setRequest("aList", aList);
		this.setRequest("bList", bList);
		this.forward("/user/autoAuctionList.jsp");
	}

	/***
	 * 根据ID删除
	 */
	public void deleteById() throws Exception
	{
		int id=0;
		try
		{
			id = Integer.parseInt(request.getParameter("id"));
		}
		catch(Exception ex)
		{

		}
		User user = this.getCurrentUser();
		int userId = user.getId();
		AutoAuction autoAuction = autoAuctionJdbc.getById(id);
		if(autoAuction.getState()==1)
		{
			AutoAuctionUtil.remove(userId+"||"+autoAuction.getProductId());
		}
		autoAuctionJdbc.deleteById(id,userId);
		
	
		
		
		this.redirect("/user/get-autoAuction-list.ac");
	}

	/****
	 * 设置状态
	 */
	public void setState() throws Exception
	{
		int id=0;
		int state = 0;
		try
		{
			id = Integer.parseInt(request.getParameter("id"));
			state = Integer.parseInt(request.getParameter("state"));
		}
		catch(Exception ex)
		{

		}
		User user = this.getCurrentUser();
		int userId = user.getId();
		autoAuctionJdbc.setState(id, state,userId);
		
		
		AutoAuction autoAuction = autoAuctionJdbc.getById(id);
		if(autoAuction.getState()==1)
		{
			AutoAuctionUtil.add(userId+"||"+autoAuction.getProductId(),autoAuction);
		}
		else
		{
			AutoAuctionUtil.remove(userId+"||"+autoAuction.getProductId());
		}
		
		
		this.redirect("/user/get-autoAuction-list.ac");
		
		
	}

	/***
	 * initAdd
	 */
	public void initEdit() throws Exception
	{
		int productId = 0;
		try
		{
			productId = Integer.parseInt(request.getParameter("productId"));
		}
		catch(Exception ex)
		{

		}
		User user = this.getCurrentUser();

		AutoAuction autoAuction = autoAuctionJdbc.getByProductId(user.getId(),productId);
		if(autoAuction==null)
		{
			autoAuction = new AutoAuction();
			autoAuction.setProductId(productId);
			autoAuction.setTactics(0);
		}
		this.setRequest("autoAuction",autoAuction);


		List<Product> productList = AuctionUtil.getList(0, 2);
		this.setRequest("productList",productList);
		this.forward("/user/autoAuctionEdit.jsp");
	}

	/***
	 * 编辑完毕
	 */
	public void editOk() throws Exception
	{
		User user = this.getCurrentUser();
		int autoAuctionId = 0;
		try
		{
			autoAuctionId = Integer.parseInt(this.input("autoAuctionId"));
		}
		catch(Exception ex)
		{

		}
		int userId = user.getId();
		int bidNum = Integer.parseInt(this.input("bidNum"));
		double startPrice = Double.parseDouble(this.input("startPrice"));
		int productId = Integer.parseInt(this.input("productId"));
		int tactics = Integer.parseInt(this.input("tactics"));

		boolean exist = autoAuctionJdbc.checkExsit(autoAuctionId, userId, productId);
		if(exist)
		{
			this.print("exist");
			return;
		}

		AutoAuction autoAuction = new AutoAuction();
		autoAuction.setProductId(productId);
		autoAuction.setId(autoAuctionId);
		autoAuction.setTime(System.currentTimeMillis());
		autoAuction.setUserId(userId);
		autoAuction.setBidNum(bidNum);
		autoAuction.setTactics(tactics);
		autoAuction.setStartPrice(startPrice);

		int biddingCount = userAccountJdbc.getBiddingCount(userId);
		if(biddingCount<bidNum)
		{
			this.print("lackBid");
			return;
		}
		if(autoAuctionId==0)
		{
			autoAuctionJdbc.save(autoAuction);
		}
		else
		{
			autoAuctionJdbc.update(autoAuction);
		}
		
		AutoAuction aAuction = autoAuctionJdbc.getByProductId(userId, productId);
		if(aAuction.getState()==1)
		{
			AutoAuctionUtil.add(userId+"||"+autoAuction.getProductId(),autoAuction);
		}
		else
		{
			AutoAuctionUtil.remove(userId+"||"+autoAuction.getProductId());
		}
		
		
		this.print("success");
	}
	
	/***
	 * 设置状态
	 */
	public void setAutoAuctionState() throws Exception
	{
		int state = 0;
		try
		{
			state = Integer.parseInt(this.input("state"));
		}
		catch(Exception ex)
		{
			
		}
		User user = this.getCurrentUser();
		int userId = user.getId();
		List<AutoAuction> aList = autoAuctionJdbc.getList(userId, 1);
		if(state==1&&aList.size()==0)
		{
			this.print("noProduct");
			return;
		}
		UserJdbc userJdbc = (UserJdbc)this.getJdbc(UserJdbc.class);
		userJdbc.setAutoAuctionState(user.getId(), state);
		 this.setSession("autoAuctionState", state);
		if(state==1)
		{
			for(int i=0;i<aList.size();i++)
			{
				AutoAuction autoAuction = (AutoAuction)aList.get(i);
				AutoAuctionUtil.add(userId+"||"+autoAuction.getProductId(), autoAuction);
			}
			Thread autoTread = new Thread(Constants.autoAuctionThread,userId+"");
			AutoThreadUtil.add(userId, autoTread);
			autoTread.start();
			
			if(logger.isDebugEnabled())
				logger.debug("启动竞拍线程->"+user.getUserName());
		}
		else
		{
			for(int i=0;i<aList.size();i++)
			{
				AutoAuction autoAuction = (AutoAuction)aList.get(i);
				AutoAuctionUtil.remove(userId+"||"+autoAuction.getProductId());
			}
			Thread autoTread = AutoThreadUtil.get(user.getId());
			if(autoTread!=null)
			{
				AutoThreadUtil.remove(user.getId());
				logger.debug("停止竞拍线程->"+user.getUserName());
			}
		}
	}
}
